package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.widget.RadioButton;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 29设置终端报警允许/禁止命令
 * Created by zwz on 2020/4/22.
 */
public class Cmd_29Fragment extends BaseFragment {




    @BindView(R.id.rbtn_open)
    RadioButton rbtnOpen;
    @BindView(R.id.rbtn_close)
    RadioButton rbtnClose;


    public static Cmd_29Fragment newInstance() {
        Bundle args = new Bundle();
        Cmd_29Fragment fragment = new Cmd_29Fragment();
        fragment.setArguments(args);


        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_29;
    }

    @Override
    protected void initView() {


    }




    private void set() {
        //预留1字节
        //说明： 允许/禁止： 1 字节， 0xAA 允许报警； 0x55 禁止报警。 RTU 的默认为允许。
        String enable = rbtnOpen.isChecked() ? "AA" : "55";
        sendData("00 " + enable);

    }

    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_29, data);

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();

                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_29)) {
                    if (data[13].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }


    @OnClick(R.id.btn_set)
    public void onViewClicked() {
        set();
    }
}
